package eg.edu.eelu.fyp2013.jdetector.core.filters.impl;

import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import eg.edu.eelu.fyp2013.jdetector.core.filters.SinglePixelFilter;
public class ContrastStretching_old implements SinglePixelFilter {
	

	/** @see AbstractWindowFilter#applyFilter(BufferedImage)
	 * Someone should implement this like AbstractWindowFilter#applyFilter(BufferedImage)
	 */
	

	private int[] upperLimits ={0, 0, 0};
	private int[] lowerLimits ={255, 255, 255};
	     
	private final int RED= 0;
	private final int GREEN= 1;
	private final int BLUE= 2;

	/**
	 * @author Moshen
	 * @author Nafisa
	 */
	@Override
	public Image applyFilter(BufferedImage inImage) {
		BufferedImage outImage = new BufferedImage(inImage.getWidth(), inImage.getHeight(), BufferedImage.TYPE_INT_RGB/*inImage.getType()*/);
		
		int tempPixelColor;
		int red, green, blue;

		for (int i = 0; i < outImage.getHeight(); i++) {
			for (int j = 0; j < outImage.getWidth(); j++) {
				tempPixelColor =inImage.getRGB(j, i);
				red = (0xFF & tempPixelColor>>16);
				if (red > upperLimits[RED]) {
					upperLimits[RED]=red;
				}
				if (red < lowerLimits[RED]) {
					lowerLimits[RED]=red;
				}
				green = (0xFF & tempPixelColor>>8);
				if (green > upperLimits[GREEN]) {
					upperLimits[GREEN]=green;
				}
				if (green < lowerLimits[GREEN]) {
					lowerLimits[GREEN]=green;
				}
				blue = (0xFF & tempPixelColor);
				if (blue > upperLimits[BLUE]) {
					upperLimits[BLUE]= blue;
				}
				if (blue < lowerLimits[BLUE]) {
					lowerLimits[BLUE]=blue;
				}
			}
		}
		for (int i = 0; i < outImage.getHeight(); i++) {
			for (int j = 0; j < outImage.getWidth(); j++) {
				tempPixelColor =inImage.getRGB(j, i);
				red = (0xFF & tempPixelColor>>16);
				green = (0xFF & tempPixelColor>>8);
				blue = (0xFF & tempPixelColor);
				outImage.setRGB(j, i, (getFilteredPixel(red, RED)<<16)|getFilteredPixel(green, GREEN)<<8|getFilteredPixel(blue, BLUE));
			}
		}
		return outImage;
	}	

	@Override 
	public int getFilteredPixel(int originalPixel, int colorPlane) {
		int newPixleValue = ( (originalPixel-lowerLimits[colorPlane]) * (255/(upperLimits[colorPlane]-lowerLimits[colorPlane])) + upperLimits[colorPlane] ); 
		//UperLimit and Lower Limit should be gotten with respect to the imported image colors range
		//newValue = 255 * (oldValue - minValue)/(maxValue - minValue)
		//tab ma te3mil getters & setters ya fale7
		//alternatively, they could be calculated in applyFilter(BufferedImage)
		
		//TODO Ahmed
		return newPixleValue;
		//Ahmed
	}

	int computeOutput (int x, int r1, int s1, int r2, int s2, int max)
	{
		float result = 0;
		if(0<= x && x <=r1)
			result =(s1/r1)*x;
		else if(r1<x && x<=r2)
			result =((s2-s1) / (r2 -r1)) * (x-r1)+s1;
		else if(r2<x && x<= max)
			result =((max - s2)/ (max - r2))*(x-r2) + s2;
		return (int)result;
	}
	/**
	 * @author Mohsen
	 *
	 */
	@Override 
	public Color getFilteredPixel(Color originalPixel) {
	
		int r = originalPixel.getRed();
		    r = getFilteredPixel(r,RED);
		
		int g = originalPixel.getGreen(); 
		    g = getFilteredPixel(g, GREEN);

		int b = originalPixel.getBlue();
		    b = getFilteredPixel(b, BLUE);
		    
		Color newColor = new Color (r, g, b);      
		
	return newColor;  
	}

}
